package mdm.led.controller;

import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.three.CureIssueList;
import mdm.led.entity.three.InsertCureIssue;
import mdm.led.entity.three.ReceiIissueMan;
import mdm.led.entity.three.ThreeCureIssue;
import mdm.led.service.ThreeTableService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年12月20日
 * @explain :
 */
@Controller
@RequestMapping(value = "threeCureIssueApp")
@Api(description = "督查三查--整改问题app")
public class ThreeCureIssueAppController {

	private Logger logger = Logger.getLogger(ThreeCureIssueAppController.class);
	@Autowired
	private ThreeTableService threeTableService;

	@ApiOperation(value = "添加整改问题")
	@RequestMapping(value = "addThreeCureIssue", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse addThreeCureIssue(@RequestBody InsertCureIssue issue) {
		logger.info("\n======== 添加整改问题  ========\n");
		boolean flag = false;
		try {
			Integer addThreeCureIssue = threeTableService.addThreeCureIssue(issue);

			flag = (addThreeCureIssue > 0) ? true : false;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, (flag == true ? "成功" : "失败"));
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, (flag == true ? "成功" : "失败"));
		}
	}

	@ApiOperation(value = "修改整改问题")
	@RequestMapping(value = "updateThreeCureIssue", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse updateThreeCureIssue(@RequestBody ThreeCureIssue issue) {
		logger.info("\n======== 修改整改问题  ========\n");
		boolean flag = false;
		try {
			Integer updateCureIssue = threeTableService.updateThreeCureIssue(issue);

			flag = (updateCureIssue > 0) ? true : false;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, (flag == true ? "成功" : "失败"));
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, (flag == true ? "成功" : "失败"));
		}
	}

	@ApiOperation(value = "查询当前用户待整改列表", notes = "入参:<br>userID : 当前用户ID")
	@RequestMapping(value = "findThreeCureIssueList", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeCureIssueList(@RequestParam("userID") Integer userID) {

		logger.info("\n======== 查询当前用户待整改列表  ========\n");
		boolean flag = false;
		List<CureIssueList> issueList = null;
		try {
			issueList = threeTableService.findThreeCureIssueList(userID);
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(issueList) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, issueList);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "根据问题ID 查询自查信息", notes = "入参:<br>cureID : 问题ID<br>selfTaskID:自查任务ID")
	@RequestMapping(value = "findThreeTaskSelfInfoByID", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeTaskSelfInfoByID(@RequestParam("cureID") Integer id,
			@RequestParam("selfTaskID") Integer selfTaskID) {
		logger.info("\n======== 根据问题ID 查询自查信息  ========\n");
		boolean flag = false;
		Map<String, Object> map = null;
		try {
			map = threeTableService.findThreeTaskSelfByID(selfTaskID);
			if (map != null) {
				map.put("cureID", id);
			}
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(map) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, map);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "根据问题ID 查询问题详情信息", notes = "入参:<br>cureID : 问题ID")
	@RequestMapping(value = "findThreeCureIssueByID", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeCureIssueByID(@RequestParam("cureID") Integer id) {
		logger.info("\n======== 根据问题ID 查询问题详情信息  ========\n");
		boolean flag = false;
		ThreeCureIssue cureIssue = null;
		try {
			cureIssue = threeTableService.findThreeCureIssueByID(id);
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(cureIssue) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, cureIssue);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "查询问题接收人", notes = "入参:<br>selfTaskID:自查任务ID <br>riskGrade:风险等级 <br>userID :自查人ID")
	@RequestMapping(value = "findReceiIissueMan", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findReceiIissueMan(Integer threeID, String riskGrade, Integer userID) {
		logger.info("\n======== 查询问题接收人  ========\n");
		boolean flag = false;
		List<ReceiIissueMan> list = null;
		try {
			list = threeTableService.findReceiIissueMan(threeID, riskGrade, userID);
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(list) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}
}
